package com.ken.blog.domain;

import com.ken.blog.domain.Article;
import com.ken.blog.domain.Category;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.persistence.Table;
import javax.transaction.Transactional;
import java.util.List;

@Table(name = "article")
@Qualifier("articleRepository")
public interface ArticleRepository extends JpaRepository<Article, Long> {
    Article findOne(Long id);

    Article save(Article a);

    Page<Article> findAll(Pageable pageable);

    List findAllByCategory(Category category);

    Page<Article> findByCategory(Category category, Pageable pageable);

    @Modifying
    @Transactional
    @Query("update Article a set a.title = ?1,a.body = ?2,a.category = ?3 where a.id = ?4")
    int update(String title, String body, Category category, Long id);
}
